package com.lgkj.ui.manager;

import android.app.ActivityOptions;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.ShopAmbientAdapter;
import com.lgkj.bean.AmbientBean;
import com.lgkj.ui.ImageBrowseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.TitleBuilder;
import com.lgkj.view.SelectPicPopupWindow;

import org.devio.takephoto.app.TakePhotoActivity;
import org.devio.takephoto.model.TResult;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * 店铺环境
 * Created by Administrator on 2018/6/25 0025.
 */

public class ShopAmbientActivity extends TakePhotoActivity {
    private static final String TAG = "ShopAmbientActivity";
    @BindView(R.id.gv_item)
    ListView gvItem;
    @BindView(R.id.iv_img)
    ImageView iv_img;
    private SelectPicPopupWindow menuWindow;
    private ShopAmbientAdapter adapter;
    private AmbientBean dataBean;
    private String imageId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_ambient;
    }

    @Override
    public void initView() {
        adapter = new ShopAmbientAdapter(ShopAmbientActivity.this);
        gvItem.setAdapter(adapter);
    }

    @Override
    public void initData() {
        initDatas();
        new TitleBuilder(this).setTitleText("店铺环境").setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        adapter.setClick(new ShopAmbientAdapter.MyClickListener() {
            @Override
            public void clickListener(View v) {
                int id = Integer.parseInt(v.getTag().toString());
                if (v.getId() == R.id.iv_del) {
                    Log.i(TAG, "clickListener: " + adapter.getItem(id).getId());
                    del(adapter.getItem(id).getId());
                }
            }
        });
        gvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i == (adapterView.getChildCount() - 1)) {
                    menuWindow = new SelectPicPopupWindow(ShopAmbientActivity.this, itemsOnClick);
//        menuWindow.sethideTake();
                    //显示窗口
                    menuWindow.showAtLocation(ShopAmbientActivity.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                } else {
                    //放大
                    Intent intent = new Intent(ShopAmbientActivity.this, ImageBrowseActivity.class);
                    intent.putExtra("image_url", adapter.getItem(i).getBarbershop_img_id());
                    startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(ShopAmbientActivity.this, gvItem, "shareNames").toBundle());
                }
            }
        });
    }

    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.btn_take_photo:
                    CommonUtils.takeFromCapture(getTakePhoto(), 350, 750);
                    break;
                case R.id.btn_pick_photo:
                    CommonUtils.takeFromDocuments(getTakePhoto(), 350, 750);
                    //调用系统图库的意图
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        String url = CommonConstant.getTakeImagePath();
        Log.i(TAG, "showImg: " + url);
        upload(new File(url));
//        Glide.with(ShopAmbientActivity.this).load(new File(url)).into(iv_img);
    }

    private void upload(File file) {
        showDiaLog("图片正在上传");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopAmbientActivity.this));
        HttpUtils.uploadImage(map, CommonConstant.uploadImg, file, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                toastLong("上传成功", 0);
                imageId = jsonObject.optString("id");
                save();
            }

            @Override
            public void errorHttp(String msg) {
                toastLong("上传失败", 0);
            }
        });
    }

    private void save() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopAmbientActivity.this));
        map.put("barbershop_id", CommonUtils.getShopId(ShopAmbientActivity.this));
        map.put("barbershop_img_id", imageId);
        HttpUtils.postHttp(map, CommonConstant.shopambientadd, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                toastLong(jsonObject.optString("msg"), 0);
                if (jsonObject.optInt("code") == 0) {
                    initDatas();
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    private void initDatas() {
        showDiaLog("正在刷新数据");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopAmbientActivity.this));
        map.put("barbershop_id", CommonUtils.getShopId(ShopAmbientActivity.this));
        HttpUtils.postHttp(map, CommonConstant.shopambientlist, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    dataBean = mGson.fromJson(String.valueOf(jsonObject), AmbientBean.class);
                    if (dataBean != null) {
                        adapter.setData(dataBean.getData());
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    /**
     * http://mda.lianguangtech.com/api/Barbershopimg/delBarbershopimg?access_token=80a0d224500097b389590ce3d2c2ce17&id=287&sid=35d74b9faa1550c010484802469748b1
     *
     * @param id
     */
    private void del(String id) {
        showDiaLog("正在删除数据");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopAmbientActivity.this));
        map.put("id", id);
        HttpUtils.postHttp(map, CommonConstant.shopambientdel, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    initDatas();
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

}
